<template>
  <div class="right-page">
    <Search 
      @searchBlog="searchBlog" 
      @changeTag="changeSearchTag"
      @changeValue="changeSearchValue"
      @clearList="clearList"/>

    <div class="blog-recommend-group" v-show="searchTag != 'blog'">
      <div class="recommend-blog">
        <recommend-blog :async_list="asyncBlogList"/>
      </div>
      <div class="recommend-user-blog">
        <recommend-blog-user :async_list="asyncBlogUserList"/>
      </div>
    </div>

    <div v-show="searchTag == 'blog'" class="blog-search-list show-in-pc">
      <div class="blog-search-tip">博客搜索只能搜索到公开博客！</div>

      <!-- 博客列表 -->
      <div
        v-infinite-scroll="loadMore"
        :infinite-scroll-disabled="loadMoreDisable"
        infinite-scroll-distance='10'>
        <div v-for="(item, index) in searchList" :key="index">
          <blog-list-item :item="item" />
        </div>
      </div>

      <list-loading v-if="listLoading"/>
      <list-end v-if="listTotal && listTotal == searchList.length && searchList.length > 6"/>
    </div>
  </div>
</template>

<script>
import Api from '@/utils/api.js'
import Search from './Search.vue'
import RecommendBlog from './RecommendBlog.vue'
import RecommendBlogUser from './RecommendBlogUser.vue'
import BlogListItem from './BlogListItem.vue';
import ListEnd from '@/components/ListEnd.vue';
import ListLoading from '@/components/ListLoading.vue';
export default {
  
  props: ["asyncBlogList", "asyncBlogUserList"],

  components:{
    Search,
    ListEnd,
    ListLoading,
    RecommendBlog,
    RecommendBlogUser,
    BlogListItem
  },  

  data(){
    return {
      searchTag: 'baidu',
      searchKey:'',
      searchValue:'',

      listLoading: false,
      listTotal:'',
      pageNum: 1,
      searchList: [],
    }
  },

  computed:{
    loadMoreDisable(){
      return this.searchList.length == 0 || this.listTotal < this.pageNum * 10
    }
  },

  methods: {
    changeSearchValue(searchValue){
      if(this.searchValue != searchValue){
        this.searchValue = searchValue
        this.clearList()
      }
    },

    changeSearchTag(tagName){
      if(this.searchTag != tagName){
        this.searchTag = tagName
      }
    },

    loadMore(){
      this.listLoading = true
      this.pageNum = this.pageNum + 1
      if((this.pageNum - 1) * 10 < this.listTotal){
        this.loadList()
      }
    },

    searchBlog(searchKey){
      if(searchKey){
        this.searchKey = searchKey
        this.pageNum = 1
        this.loadList()
      }else{
        this.$message.warning("请输入搜索关键字");
      }
    },

    loadList(){     
      // 请求接口
      this.$axios.post(Api.SEARCH_BLOG, {
        pageNum: this.pageNum,
        searchKey: this.searchKey
      }).then( res =>{
        if(this.pageNum == 1){
          this.searchList = res.list
        }else{
          this.searchList.push(...res.list)
        }
        this.listTotal = res.total
        this.listLoading = false
      }).catch(res =>{
        this.$message.warning(res.message);
      })
    },

    clearList(){
      this.pageNum = 1
      this.searchList = []
      this.searchKey = ''
      this.listLoading = false
    }
  }
}
</script>

<style lang="less" scoped>
@import '@/assets/css/index.less';

.right-page {
  font-size: 18px;
  color: #353535;
  height: 100%;
  .flex-center-start();
  flex-direction: column;

  .blog-recommend-group{
    .flex-start-center();
    padding-top: 150px;

    .recommend-user-blog{
      width: 300px;
      margin-left: 50px;
    }

    .recommend-blog{
      width: 700px;
      margin-left: 350px;
    }
  }

  .blog-search-list{
    width: 700px;

    .blog-search-tip{
      font-size: 12px;
      color: #aaaaaa;
      margin: 10px 0;
    }
  }
}

@media screen and (max-width: 1023px) {
  .right-page {
    width: 100vw;
    display: inline-block;

    .blog-recommend-group{
      display: inline-block;
      padding-top: 0px;

      .recommend-user-blog{
        width: 100vw;
        margin-left: 0px;
      }

      .recommend-blog{
        width: 100vw;
        margin-left: 0px;
      }
    }
  }
}
</style>
